package com.make.house.api.utils;

/**
 * Created by xujun on 2017/11/20.
 * 16进制和2进制的转换
 */
public class HexUtils {

    public HexUtils() {
    }
    public static String byte2hex(byte[] b) {
        StringBuilder sb = new StringBuilder();
        String tmp = "";

        for(int i = 0; i < b.length; ++i) {
            tmp = Integer.toHexString(b[i] & 255);
            if(tmp.length() == 1) {
                sb.append("0");
            }

            sb.append(tmp);
        }

        return sb.toString();
    }

    public static byte[] hex2byte(String str) {
        byte[] bytes = new byte[str.length() / 2];

        for(int i = 0; i < bytes.length; ++i) {
            bytes[i] = (byte)Integer.parseInt(str.substring(i * 2, i * 2 + 2), 16);
        }

        return bytes;
    }
}
